package edu.cuit.avatar.callback;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/10 15:51
 */
public class Talking {
    //定时器周期执行时间(毫秒值)
    private int interval;
    private boolean beep;

    public Talking(int interval, boolean beep) {
        this.interval = interval;
        this.beep = beep;
    }

    /**
     * 启动定时器
     */
    public void start(){
        var timer = new Timer(interval, new BeepActionListener());
        timer.start();
    }

    /**
     * 成员内部类:对外进行更好的封装(隐藏)
     */
    private class BeepActionListener implements ActionListener{
        /**
         * 回调方法:当指定事件发生时触发回调，执行相关业务逻辑
         * @param e the event to be processed
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            //该事件发生时发出一个通告
            System.out.println("At the tone，the time is "
                    + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            //内部类可以直接访问外围类的成员
            if(beep)
                //伴随响铃
                Toolkit.getDefaultToolkit().beep();
        }
    }
}
